clear all

********************************************************************************
// Data for Robustness checks
********************************************************************************

// Alternative years

forvalues y=2014(2)2016{
	use "ZCTALevelData_R2.dta", clear

	keep if year==`y'

	ren state statea 
	ren county countya
	tostring statea, gen(stated)
	tostring countya, gen(countyd)
	gen z = "0"
	gen c_l = length(countyd)
	gen s_l = length(stated)
	gen state = ""
	replace state = stated if s_l==2
	egen state_input = concat(z stated), p("")
	replace state = state_input if s_l==1
	gen county2 = ""
	replace county2 = countyd if c_l==3
	egen county_input1 = concat(z countyd), p("")
	egen county_input2 = concat(z z countyd), p("")
	replace county2 = county_input1 if c_l==2
	replace county2 = county_input2 if c_l==1
	egen county = concat(state county2), p("")
	drop county2 county_input* state_inpu c_l s_l z countyd stated
	drop if state=="02"|state=="15"|state=="72"|state=="78"|state=="60"|state=="66"|state=="69"
	drop if tot_pop==.
	drop if tot_pop==0
	drop if cms_wage_index==.

	sum tot_pop, detail

	gen cat_ucc = n_urgentcare
	replace cat_ucc = 4 if cat_ucc>=4
	gen cat_hospaff_ucc = n_hospaffucc
	replace cat_hospaff_ucc = 2 if cat_hospaff_ucc>=2
	gen cat_hospaff_ucc_geo = n_hospaffucc_geo
	replace cat_hospaff_ucc_geo = 2 if cat_hospaff_ucc_geo>=2
	gen cat_hospaff_ucc_geo2 = n_hospaffucc_geo2
	replace cat_hospaff_ucc_geo2 = 2 if cat_hospaff_ucc_geo2>=2

	egen mean_inc = mean(income_pc), by(county)
	replace income_pc = mean_inc if income_pc==.
	drop mean_inc
	replace income_pc = income_pc/10000
	replace median_value = median_value/1000000
	replace median_gross_rent = median_gross_rent/1000
	gen any_hosp = (n_hospitals>0)
	gen immigration = moved_within_county+moved_within_state+moved_state+moved_abroad
	replace immigration = 0 if immigration==.
	gen gte_highschool = highschool + some_college + bachelor_higher
	replace gte_highschool = 0 if gte_highschool==.

	local demog "nonhisp_white nonhisp_black nonhisp_other hispanic gte_highschool female age_65 uninsured medicare medicaid"
	foreach dem of local demog{
		replace `dem' = `dem'/100
	}

	gen median_hisp = .
	qui sum hispanic, detail
	replace median_hisp = r(p50)
	gen above_median_hisp = (hispanic>=median_hisp)
	drop median_hisp

	gen median_black = .
	qui sum nonhisp_black, detail
	replace median_black = r(p50)
	gen above_median_black = (nonhisp_black>=median_black)
	drop median_black

	gen median_income_pc = .
	qui sum income_pc, detail
	replace median_income_pc = r(p50)
	gen high_income = (income_pc>=median_income_pc)
	drop median_income_pc

	gen median_svi = .
	qui sum rpl_themes if year==2015, detail
	replace median_svi = r(p50)
	gen high_svi = (rpl_themes>=median_svi)
	drop median_svi
	gen intercept = 1

	gen median_black = .
	qui sum nonhisp_black if high_svi==1, detail
	replace median_black = r(p50)
	gen abv_med_blck_high_svi = (nonhisp_black>=median_black) if high_svi==1
	drop median_black

	gen median_income_pc = .
	qui sum income_pc if high_svi==1, detail
	replace median_income_pc = r(p50)
	gen high_income_high_svi = (income_pc>=median_income_pc) if high_svi==1
	drop median_income_pc

	gen median_uninsured = .
	qui sum uninsured, detail
	replace median_uninsured = r(p50)
	gen high_uninsured = (uninsured>=median_uninsured)
	drop median_uninsured

	gen con_laws = .
	replace con_laws = 1 if state=="02"| state=="15"| state=="53"| state=="41"| state=="30"| state=="32"| state=="31"| state=="19"| state=="40"| state=="29"| state=="17"| state=="22"| state=="05"| state=="21"| state=="18"| state=="26"| state=="39"| state=="54"| state=="47"| state=="28"| state=="01"| state=="51"| state=="24"| state=="36"| state=="50"| state=="23"| state=="25"| state=="44"| state=="34"| state=="09"| state=="10"| state=="37"| state=="45"| state=="13"| state=="12"
	replace con_laws = 0 if con_laws==.

	gen con_laws2 = con_laws
	replace con_laws2 = 1 if state=="55"|state=="27"|state=="04"

	gen ucc_regulation = .
	replace ucc_regulation = 1 if state=="04"| state=="09"| state=="10"| state=="12"| state=="04"| state=="33"| state=="34"| state=="35"| state=="36"| state=="38"| state=="41"| state=="44"| state=="49"
	replace ucc_regulation = 0 if ucc_regulation==.

	gen con_laws3 = con_laws 
	replace con_laws3 = 0 if ucc_regulation==1

	gen con_intensity = .
	gen con_facilities = 0
	qui do "replace_con.do"
	
	replace tot_pop = tot_pop/10000
	replace tot_pop = tot_pop/1000
	gen tot_pop2 = tot_pop

	gen cat_hosp = n_hospitals
	replace cat_hosp = 1 if cat_hosp>1

	replace con_intensity = con_intensity/28
	saveold "ZCTALevelRobust_`y'.dta", version(12) replace

}


